package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.dto.TenantDto;
import cn.itsource.hrm.errorcode.ErrorCode;
import cn.itsource.hrm.exception.GlobalException;
import cn.itsource.hrm.mapper.EmployeeMapper;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.service.ITenantService;
import cn.itsource.hrm.util.ValidUtils;
import cn.itsource.hrm.util.encrypt.MD5;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xqf
 * @since 2021-01-11
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 机构入驻
     * @param tenantDto
     */
    @Override
    public void register(TenantDto tenantDto) {
        //1.接收参数
        Tenant tenant = tenantDto.getTenant();
        Employee employee = tenantDto.getEmployee();
        Long mealId = tenantDto.getMealId();
        //2.判断参数
        validParams(tenant, employee);
        /*if(StringUtils.isEmpty(tenant.getCompanyName())){
            throw new GlobalException("机构名不能为空哦");
        }
       if(!ValidUtils.isPhone(employee.getTel())){
            throw new GlobalException("手机号格式不正确");
        }
        if(!ValidUtils.isEmail(employee.getEmail())){
            throw new GlobalException("邮箱格式不正确");
        }*/
        /*  if(emp!=null){
            throw new GlobalException("该用户已经注册过");
        }*/
        Date date = new Date();
        //3.保存机构
        saveTenant(tenant, date);
        //4.保存员工
        saveEmployee(tenant, employee, date);
        //5.保存机构与套餐之间的关系
        //套餐时间
        Date expireDate = DateUtils.addDays(date, 7);
        baseMapper.saveRelationTenant(mealId,tenant.getId(),expireDate,Tenant.MEAL_UNPAID);
    }

    private void saveEmployee(Tenant tenant, Employee employee, Date date) {
        employee.setState(Employee.NORMAL);
        String salt = MD5.getRandomCode(16);
        employee.setSalt(salt);
        employee.setInputTime(date);
        employee.setDeptId(null);
        employee.setTenantId(tenant.getId());
        employee.setLoginId(null);
        employee.setType(Employee.INSTITUTIONAL_ADMINISTRATOR);
        employee.setPassword(MD5.getMD5(employee.getPassword()+salt));
        employeeMapper.insert(employee);
    }

    private void saveTenant(Tenant tenant, Date date) {
        tenant.setState(Tenant.PENDING);

        tenant.setRegisterTime(date);
        baseMapper.insert(tenant);
    }

    private void validParams(Tenant tenant, Employee employee) {
      /*  ValidUtils.isNotNull(tenant.getCompanyName(), ErrorCode.TENANT_NAME_ISNULL.getMessage());

        ValidUtils.isEmail(employee.getEmail(),ErrorCode.EMAIL_INVALID.getMessage() );*/
        ValidUtils.isPhone(employee.getTel(),ErrorCode.PHONE_INVALID.getMessage());
        //通过用户名查询用户对象
        Employee emp= employeeMapper.findByUsername(employee.getUsername());
        //判断该用户是否已经注册
        ValidUtils.isNull(emp, ErrorCode.USERNAME_ALREADY_REGISTERED.getMessage());
    }
}
